/** <a href="http://www.cpupk.com/decompiler">Eclipse Class Decompiler</a> plugin, Copyright (c) 2017 Chen Chao. **/
package com.minierp.controller;

import com.minierp.bean.OrderParameterCarrier;
import com.minierp.bean.QueryParameterCarrier;
import com.minierp.bean.QueryResult;
import com.minierp.bean.UserBean;
import com.minierp.dao.AccessoryDao;
import com.minierp.dao.BusinessBillDao;
import com.minierp.dao.IdGenerator;
import com.minierp.dao.QueryDao;
import com.minierp.dao.WorkCenterDao;
import com.minierp.helper.EncCommonHelper;
import com.minierp.model.Accessory;
import com.minierp.model.Establishment;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Vector;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

public class AccessoryMACtr extends MultiActionController {
	private String listView;
	private String listForBusiView;
	private String listAloneView;
	private String listForClientView;
	private String listForEstablishmentView;
	private String uploadView;
	private String multiUploadView;
	private String multiFormView;
	private String uploadSuccessView;
	private AccessoryDao accessoryDao;
	private BusinessBillDao businessBillDao;
	private WorkCenterDao workCenterDao;
	private IdGenerator idGenerator;
	private QueryDao queryDao;

	public ModelAndView handleList(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		String agentId = request.getParameter("agent_id");
		String extId = request.getParameter("ext_id");
		String code = request.getParameter("code");
		String editFlag = request.getParameter("edit_flag");
		String agentType = request.getParameter("agent_type");
		String accType = request.getParameter("acc_type");
		if (extId == null || extId.equals("")) {
			extId = "0";
		}

		String[] accTypeArr = (String[]) null;
		if (accType != null && !accType.equals("")) {
			accTypeArr = accType.split(";");
		}

		ModelAndView mv;
		if (agentType.equals("BUSINESS_BILL")) {
			mv = new ModelAndView(this.listForBusiView);
			mv.addObject("workCenterList", this.workCenterDao.getList(true));
			mv.addObject("accType", accType);
		} else {
			mv = new ModelAndView(this.listView);
		}

		if (agentId != null && !agentId.equals("")) {
			mv.addObject(
					"list",
					this.accessoryDao.getList(agentType,
							Long.parseLong(agentId), accTypeArr, 0L));
		} else {
			mv.addObject("list",
					this.accessoryDao.getList(agentType, code, accTypeArr, 0L));
		}

		mv.addObject("code", code);
		mv.addObject("agentId", agentId);
		mv.addObject("agentType", agentType);
		mv.addObject("editFlag", editFlag);
		mv.addObject("extId", extId);
		return mv;
	}

	public ModelAndView handleListAlone(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		ModelAndView mv = new ModelAndView(this.listAloneView);
		String agentId = request.getParameter("agent_id");
		String agentType = request.getParameter("agent_type");
		String accType = request.getParameter("acc_type");
		String mark = request.getParameter("mark");
		long extId = 0L;
		if (request.getParameter("ext_id") != null
				&& !request.getParameter("ext_id").equals("")) {
			extId = Long.parseLong(request.getParameter("ext_id"));
		}

		String[] accTypeArr = (String[]) null;
		if (accType != null && !accType.equals("")) {
			accTypeArr = new String[]{accType};
		}

		mv.addObject("list", this.accessoryDao.getList(agentType,
				Long.parseLong(agentId), accTypeArr, extId));
		mv.addObject("agentId", agentId);
		mv.addObject("agentType", agentType);
		mv.addObject("mark", mark);
		return mv;
	}

	public ModelAndView handleDelete(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		String id = request.getParameter("id");
		Accessory a = (Accessory) this.accessoryDao.get(Accessory.class,
				Long.parseLong(id));
		if (a != null && a.getFileName() != null && !a.getFileName().equals("")) {
			File returnUrl = new File(this.getServletContext().getRealPath("/")
					+ a.getFileName());
			if (returnUrl.exists() && returnUrl.isFile()) {
				returnUrl.delete();
			}
		}

		this.accessoryDao.delete(Long.parseLong(id));
		String returnUrl1 = request.getParameter("from_url");
		if (returnUrl1 != null && !returnUrl1.equals("")) {
			ModelAndView mv = new ModelAndView("redirect:" + returnUrl1);
			return mv;
		} else {
			return null;
		}
	}

	public ModelAndView handleCopy(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		String id = request.getParameter("id");
		UserBean ub = EncCommonHelper.getCurrentUserAccount(request);
		Accessory ta = (Accessory) this.accessoryDao.get(Accessory.class,
				Long.parseLong(id));
		String tFileName = ta.getFileName();
		String extendName = EncCommonHelper.getExtendName(tFileName);
		String uploadPath = EncCommonHelper.getUploadVirtualPath(this
				.getServletContext());
		String realPath = this.getServletContext().getRealPath(uploadPath);
		String fileName = ub.getStaffer().getId() + "_"
				+ System.currentTimeMillis() + "." + extendName;
		File tf = new File(this.getServletContext().getRealPath("/")
				+ System.getProperty("file.separator") + tFileName);
		String returnUrl;
		if (tf.exists() && tf.isFile()) {
			FileCopyUtils.copy(tf,
					new File(realPath + System.getProperty("file.separator")
							+ fileName));
			returnUrl = uploadPath + fileName;
			Accessory mv = new Accessory();
			mv.setAgentId(ta.getAgentId());
			mv.setAgentType(ta.getAgentType());
			mv.setCreaterId(ub.getStaffer().getId());
			mv.setCreaterName(ub.getStaffer().getStafferName());
			mv.setCreateTime(new Date());
			mv.setFileName(returnUrl);
			mv.setId(this.idGenerator.getId(Accessory.class.getName()));
			mv.setMark(ta.getMark());
			mv.setAccType(ta.getAccType());
			mv.setExtId(ta.getExtId());
			mv.setCustomPath(ta.getCustomPath());
			mv.setTmpCode(ta.getTmpCode());
			mv.setSourceName(ta.getSourceName());
			this.accessoryDao.save(mv);
		}

		returnUrl = request.getParameter("from_url");
		ModelAndView mv1 = new ModelAndView("redirect:" + returnUrl);
		return mv1;
	}

	public ModelAndView handleListForClient(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		ModelAndView mv = new ModelAndView(this.listForClientView);
		String clientId = request.getParameter("id");
		String billObjName = "com.minierp.model.BusinessBill";
		String billObjShortName = "b";
		String beObjName = "com.minierp.model.BusinessBillExt";
		String beObjShortName = "be";
		String acceObjName = "com.minierp.model.Accessory";
		String acceObjShortName = "a";
		HashMap shortNameMap = new HashMap();
		String[] retObjs = new String[]{acceObjShortName, billObjShortName};
		Vector qpVe = new Vector();
		shortNameMap.put(billObjName, billObjShortName);
		shortNameMap.put(beObjName, beObjShortName);
		shortNameMap.put(acceObjName, acceObjShortName);
		QueryParameterCarrier qp = new QueryParameterCarrier();
		qp.objectName = billObjName;
		qp.propertyName = "id";
		qp.value = " b.id=be.id";
		qp.dataType = 6;
		qp.logicType = 10;
		qpVe.add(qp);
		qp = new QueryParameterCarrier();
		qp.objectName = acceObjName;
		qp.propertyName = "agentId";
		qp.value = " a.agentId=b.id";
		qp.dataType = 6;
		qp.logicType = 10;
		qpVe.add(qp);
		qp = new QueryParameterCarrier();
		qp.objectName = acceObjName;
		qp.propertyName = "agentType";
		qp.value = " a.agentType=\'BUSINESS_BILL\'";
		qp.dataType = 6;
		qp.logicType = 10;
		qpVe.add(qp);
		qp = new QueryParameterCarrier();
		qp.objectName = billObjName;
		qp.propertyName = "clientId";
		qp.value = clientId;
		qp.dataType = 3;
		qp.logicType = 1;
		qpVe.add(qp);
		qp = new QueryParameterCarrier();
		qp.objectName = beObjName;
		qp.propertyName = "enabled";
		qp.value = " be.enabled=\'Y\'";
		qp.dataType = 6;
		qp.logicType = 10;
		qpVe.add(qp);
		SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
		Date allowStart = null;
		if (EncCommonHelper.checkPurePrivilege(request, "NO_EARL_DATA")) {
			allowStart = EncCommonHelper.getEarlDataDate(request);
			qp = new QueryParameterCarrier();
			qp.objectName = billObjName;
			qp.propertyName = "orderDate";
			qp.value = sf.format(allowStart);
			qp.dataType = 4;
			qp.logicType = 5;
			qpVe.add(qp);
		}

		QueryParameterCarrier[] qParas = new QueryParameterCarrier[qpVe.size()];

		for (int p1 = 0; p1 < qpVe.size(); ++p1) {
			qParas[p1] = (QueryParameterCarrier) qpVe.get(p1);
		}

		OrderParameterCarrier arg20 = new OrderParameterCarrier();
		arg20.objectName = acceObjName;
		arg20.propertyName = "id";
		arg20.orderTarget = "desc";
		OrderParameterCarrier[] oParas = new OrderParameterCarrier[]{arg20};
		QueryResult qr = this.queryDao.doQuery(
				EncCommonHelper.getCurrentPageIndex(request),
				EncCommonHelper.getPageSize(request), qParas, oParas,
				(String[]) null, shortNameMap, retObjs, (String[]) null);
		mv.addObject("qr", qr);
		mv.addObject(
				"est",
				this.accessoryDao.get(Establishment.class,
						Long.parseLong(clientId)));
		mv.addObject("id", clientId);
		return mv;
	}

	public ModelAndView handleListForEstablishment(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		ModelAndView mv = new ModelAndView(this.listForEstablishmentView);
		String estId = request.getParameter("est_id");
		mv.addObject(
				"list",
				this.accessoryDao.getList("ESTABLISHMENT",
						Long.parseLong(estId)));
		mv.addObject("estId", estId);
		mv.addObject(
				"est",
				this.accessoryDao.get(Establishment.class,
						Long.parseLong(estId)));
		return mv;
	}

	public ModelAndView handleUpload(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		String type = request.getParameter("type");
		String agentId = request.getParameter("agent_id");
		if (agentId == null) {
			agentId = "";
		}

		String code = request.getParameter("code");
		if (code == null) {
			code = "";
		}

		String extId = request.getParameter("ext_id");
		if (extId == null) {
			extId = "";
		}

		String agentType = request.getParameter("agent_type");
		String accType = request.getParameter("acc_type");
		String serverName = request.getServerName();
		ModelAndView mv;
		if (type != null && type.equals("MULTI")) {
			mv = new ModelAndView(this.multiUploadView);
			mv.addObject("wcList", this.workCenterDao.getList(true));
		} else if (type != null && type.equals("MULTI_FORM")) {
			mv = new ModelAndView(this.multiFormView);
			mv.addObject("wcList", this.workCenterDao.getList(true));
		} else {
			mv = new ModelAndView(this.uploadView);
		}

		mv.addObject("agentId", agentId);
		mv.addObject("type", type);
		mv.addObject("code", code);
		mv.addObject("extId", extId);
		mv.addObject("agentType", agentType);
		mv.addObject("accType", accType);
		mv.addObject("serverName", serverName);
		mv.addObject("serverPort", String.valueOf(EncCommonHelper
				.getEnterpriseInfo(request).getUploadPort()));
		return mv;
	}

	public ModelAndView handleInsertRecord(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		Accessory obj = new Accessory();
		UserBean ub = EncCommonHelper.getCurrentUserAccount(request);
		String agentId = request.getParameter("agent_id");
		String agentType = request.getParameter("agent_type");
		String fileName = request.getParameter("file_name");
		String customPath = request.getParameter("custom_path");
		String extId = request.getParameter("ext_id");
		String accType = request.getParameter("acc_type");
		String mark = request.getParameter("mark");
		String returnType = request.getParameter("return_type");
		String sourceName = "";
		int mv;
		if (fileName != null && !fileName.trim().equals("")) {
			sourceName = fileName;
			mv = fileName.lastIndexOf("/");
			if (mv != -1) {
				sourceName = fileName.substring(mv + 1);
			}
		} else if (customPath != null && !customPath.trim().equals("")) {
			customPath = customPath.trim();
			sourceName = customPath.replace("/", "\\");
			if (sourceName.endsWith("\\")) {
				sourceName = sourceName.substring(0, sourceName.length() - 1);
			}

			mv = sourceName.lastIndexOf("\\");
			if (mv != -1) {
				sourceName = sourceName.substring(mv + 1);
			}
		}

		if (sourceName != null && !sourceName.equals("")) {
			if (agentId != null && !agentId.equals("")) {
				obj.setAgentId(Long.parseLong(agentId));
			} else {
				String mv1 = request.getParameter("code");
				obj.setTmpCode(mv1);
			}

			obj.setAgentType(agentType);
			obj.setFileName(fileName);
			obj.setSourceName(sourceName);
			obj.setCustomPath(customPath);
			obj.setMark(mark);
			if (extId != null && !extId.equals("")) {
				obj.setExtId(Long.parseLong(extId));
			}

			obj.setAccType(accType);
			if (accType == null || !accType.equals("DESIGN")) {
				obj.setExtId(0L);
			}

			if (ub != null) {
				obj.setCreaterId(ub.getStaffer().getId());
				obj.setCreaterName(ub.getStaffer().getStafferName());
			}

			obj.setCreateTime(new Date());
			obj.setId(this.idGenerator.getId(Accessory.class.getName()));
			this.accessoryDao.save(obj);
		}

		if (returnType != null && returnType.equals("AJAX")) {
			response.setContentType("text/plain");
			response.setCharacterEncoding("utf-8");
			response.setHeader("Cache-Control", "no-cache");
			response.setHeader("Pragma", "no-cache");
			ServletOutputStream mv3 = response.getOutputStream();
			mv3.write(String.valueOf(obj.getId()).getBytes());
			mv3.flush();
			mv3.close();
			return null;
		} else {
			ModelAndView mv2 = new ModelAndView(this.uploadSuccessView);
			mv2.addObject("returnValue", String.valueOf(obj.getId()));
			return mv2;
		}
	}

	public ModelAndView handleMultiNew(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		ModelAndView mv = new ModelAndView(this.uploadSuccessView);
		UserBean ub = EncCommonHelper.getCurrentUserAccount(request);
		String uploadType = request.getParameter("upload_type");
		String agentId = request.getParameter("agentId");
		String code = request.getParameter("code");
		String agentType = request.getParameter("agentType");
		String uploadPath;
		String realPath;
		if (uploadType.equals("PATH")) {
			String multipartRequest = request.getParameter("customPath");
			uploadPath = request.getParameter("customMark");
			realPath = request.getParameter("extId");
			String i = request.getParameter("accType");
			multipartRequest = multipartRequest.trim();
			String multipartFile = multipartRequest.replace("/", "\\");
			if (multipartFile.endsWith("\\")) {
				multipartFile = multipartFile.substring(0,
						multipartFile.length() - 1);
			}

			int sourceFileName = multipartFile.lastIndexOf("\\");
			if (sourceFileName != -1) {
				multipartFile = multipartFile.substring(sourceFileName + 1);
			}

			Accessory mark = new Accessory();
			if (agentId != null && !agentId.equals("")) {
				mark.setAgentId(Long.parseLong(agentId));
			} else {
				mark.setTmpCode(code);
			}

			mark.setAgentType(agentType);
			mark.setSourceName(multipartFile);
			mark.setCustomPath(multipartRequest);
			mark.setMark(uploadPath);
			if (realPath != null && !realPath.equals("")) {
				mark.setExtId(Long.parseLong(realPath));
			}

			mark.setAccType(i);
			if (i == null || !i.equals("DESIGN")) {
				mark.setExtId(0L);
			}

			mark.setCreaterId(ub.getStaffer().getId());
			mark.setCreaterName(ub.getStaffer().getStafferName());
			mark.setCreateTime(new Date());
			mark.setId(this.idGenerator.getId(Accessory.class.getName()));
			this.accessoryDao.save(mark);
		} else {
			MultipartHttpServletRequest arg39 = (MultipartHttpServletRequest) request;
			uploadPath = EncCommonHelper.getUploadVirtualPath(this
					.getServletContext());
			realPath = this.getServletContext().getRealPath(uploadPath);

			for (int arg40 = 1; arg40 < 6; ++arg40) {
				MultipartFile arg42 = arg39.getFile("fileName" + arg40);
				String arg41 = request.getParameter("hid_file_name" + arg40);
				String arg43 = request.getParameter("mark" + arg40);
				String extId = request.getParameter("extId" + arg40);
				String accType = request.getParameter("accType" + arg40);
				if (arg42 != null && !arg42.isEmpty() && arg41 != null
						&& !arg41.equals("")) {
					arg41 = arg41.replace("/", "\\");
					String[] tmpArr = arg41.split("\\\\");
					String fileName = tmpArr[tmpArr.length - 1];
					int lastIndex = fileName.lastIndexOf(".");
					String partName = fileName;
					String extendName = "";
					if (lastIndex != -1) {
						extendName = fileName.substring(lastIndex + 1);
						partName = fileName.substring(0, lastIndex);
					}

					File f = new File(realPath
							+ System.getProperty("file.separator") + fileName);

					for (int index = 0; f.exists() && f.isFile(); f = new File(
							realPath + System.getProperty("file.separator")
									+ fileName)) {
						++index;
						fileName = partName + "(" + index + ")." + extendName;
					}

					InputStream in = null;
					FileOutputStream out = null;

					try {
						in = arg42.getInputStream();
						out = new FileOutputStream(f);

						byte[] b;
						for (boolean obj = true; obj; b = (byte[]) null) {
							b = new byte[1024];
							int num = in.read(b);
							if (num > -1) {
								out.write(b);
							} else {
								obj = false;
							}
						}

						out.close();
						in.close();
					} finally {
						try {
							if (in != null) {
								in.close();
							}
						} catch (Exception arg37) {
							;
						}

						try {
							if (out != null) {
								out.close();
							}
						} catch (Exception arg36) {
							;
						}

					}

					Accessory arg44 = new Accessory();
					if (agentId != null && !agentId.equals("")) {
						arg44.setAgentId(Long.parseLong(agentId));
					} else {
						arg44.setTmpCode(code);
					}

					arg44.setAgentType(agentType);
					arg44.setSourceName(fileName);
					arg44.setFileName(uploadPath + fileName);
					arg44.setMark(arg43);
					if (extId != null && !extId.equals("")) {
						arg44.setExtId(Long.parseLong(extId));
					}

					arg44.setAccType(accType);
					if (accType == null || !accType.equals("DESIGN")) {
						arg44.setExtId(0L);
					}

					arg44.setCreaterId(ub.getStaffer().getId());
					arg44.setCreaterName(ub.getStaffer().getStafferName());
					arg44.setCreateTime(new Date());
					arg44.setId(this.idGenerator.getId(Accessory.class
							.getName()));
					this.accessoryDao.save(arg44);
				}
			}
		}

		mv.addObject("returnValue", "ok");
		return mv;
	}

	public AccessoryDao getAccessoryDao() {
		return this.accessoryDao;
	}

	public void setAccessoryDao(AccessoryDao accessoryDao) {
		this.accessoryDao = accessoryDao;
	}

	public String getListView() {
		return this.listView;
	}

	public void setListView(String listView) {
		this.listView = listView;
	}

	public String getListAloneView() {
		return this.listAloneView;
	}

	public void setListAloneView(String listAloneView) {
		this.listAloneView = listAloneView;
	}

	public String getListForClientView() {
		return this.listForClientView;
	}

	public void setListForClientView(String listForClientView) {
		this.listForClientView = listForClientView;
	}

	public QueryDao getQueryDao() {
		return this.queryDao;
	}

	public void setQueryDao(QueryDao queryDao) {
		this.queryDao = queryDao;
	}

	public IdGenerator getIdGenerator() {
		return this.idGenerator;
	}

	public void setIdGenerator(IdGenerator idGenerator) {
		this.idGenerator = idGenerator;
	}

	public String getUploadSuccessView() {
		return this.uploadSuccessView;
	}

	public void setUploadSuccessView(String uploadSuccessView) {
		this.uploadSuccessView = uploadSuccessView;
	}

	public String getUploadView() {
		return this.uploadView;
	}

	public void setUploadView(String uploadView) {
		this.uploadView = uploadView;
	}

	public BusinessBillDao getBusinessBillDao() {
		return this.businessBillDao;
	}

	public void setBusinessBillDao(BusinessBillDao businessBillDao) {
		this.businessBillDao = businessBillDao;
	}

	public String getListForEstablishmentView() {
		return this.listForEstablishmentView;
	}

	public void setListForEstablishmentView(String listForEstablishmentView) {
		this.listForEstablishmentView = listForEstablishmentView;
	}

	public String getMultiUploadView() {
		return this.multiUploadView;
	}

	public void setMultiUploadView(String multiUploadView) {
		this.multiUploadView = multiUploadView;
	}

	public WorkCenterDao getWorkCenterDao() {
		return this.workCenterDao;
	}

	public void setWorkCenterDao(WorkCenterDao workCenterDao) {
		this.workCenterDao = workCenterDao;
	}

	public String getMultiFormView() {
		return this.multiFormView;
	}

	public void setMultiFormView(String multiFormView) {
		this.multiFormView = multiFormView;
	}

	public String getListForBusiView() {
		return this.listForBusiView;
	}

	public void setListForBusiView(String listForBusiView) {
		this.listForBusiView = listForBusiView;
	}
}